package com.qk.ezbuy.controller;

import com.github.pagehelper.PageInfo;
import com.qk.ezbuy.pojo.News;
import com.qk.ezbuy.pojo.UserAddress;
import com.qk.ezbuy.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/MemberAddress")
public class UserAddressController {
    @Autowired
    private UserAddressService userAddressService;

    /**
     * 增加
     * @param userAddress
     * @return
     */
    @RequestMapping("/Address")
    public Map Address( UserAddress userAddress){
        Map<String,Object> map = new HashMap<String,Object>();
        userAddress.setCreatetime(new Date());
        System.out.println(userAddress.getMobile());
        int count = userAddressService.insertSelective(userAddress);
        if(count>0){
            map.put("msg",1);
        }else{
            map.put("msg",2);
        }
        return map;
    }
    @RequestMapping("selectUserAddressByuserId")
    public List selectUserAddressByuserId(int userid){
        System.out.println(userid+"===");
        List<UserAddress> userAddressList = userAddressService.selectUserAddressById(userid);
        return userAddressList;
    }

    //查默认地址
    @RequestMapping("selectUserAddressByisDefault")
    public Map selectUserAddressByisDefault(int userid){

        System.out.println(userid+"==================");
        Map<String,Object> map = new HashMap<String,Object>();
        UserAddress userAddress = userAddressService.selectUserAddressByisDefault(userid);
        map.put("userAddress",userAddress);
        return map;
    }
    /**
     * 地址分页
     */
    @RequestMapping("selectPage")
    public PageInfo<UserAddress> selectByPage(@RequestParam int pageNum, @RequestParam int pageSize,@RequestParam int userid){
        PageInfo<UserAddress> pageInfo=null;
        pageInfo=userAddressService.selectPage(pageNum,pageSize,userid);
        return pageInfo;
    }
    /**
     * 地址修改
     */
    @RequestMapping("updateUserAddress")
    public Map updateUserAddress(UserAddress userAddress){
        Map<String,Object> map = new HashMap<String,Object>();
        int count = userAddressService.updateByPrimaryKeySelective(userAddress);
        if(count>0){
            map.put("msg",1);
        }else{
            map.put("msg",2);
        }
        return map;
    }
    /**
     * 根据地址id修改回显单个地址
     */
    @RequestMapping("selectUserAddressById")
    public Map selectUserAddressById(int id){
        Map<String,Object> map = new HashMap<String,Object>();
        UserAddress userAddress = userAddressService.selectByUserAddressId(id);
        map.put("userAddress",userAddress);
        return map;
    }

    /**
     * 逻辑删除单个地址
     */
    @RequestMapping("delUserAddressById")
    public Map delUserAddressById(UserAddress userAddress){
        Map<String,Object> map = new HashMap<String,Object>();
        userAddress.setIsdefault(2);
        int count = userAddressService.delUserAddressById(userAddress);
        if(count>0){
            map.put("msg",1);
        }else{
            map.put("msg",2);
        }
        return map;
    }
    @RequestMapping("UpdateUserAddressMoRen")
    public String UpdateUserAddressMoRen(int id,int userid){
        UserAddress userAddress = new UserAddress();
        Map<String,Object> map = new HashMap<String,Object>();
        List<UserAddress> userAddress1 = userAddressService.selectAll(userid);
        for(int i = 0; i < userAddress1.size(); i++){
            UserAddress u = userAddress1.get(i);
            if(u.getIsdefault()==1 && u.getIsdefault() !=2) {
                u.setIsdefault(0);
                userAddressService.updateByPrimaryKeySelective(u);
            }
        }
        userAddress.setId(id);
        userAddress.setIsdefault(1);
        userAddressService.updateByPrimaryKeySelective(userAddress);
        return "成功";
    }
}
